home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / Frontier 4.0.1 / Frontier SDK 4.0b2 / Extras / Droplet Developer 4.0 / C Source / droplet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  27.0 KB  |  1,534 lines  |  [TEXT/CWIE]

  1.  
  2. /*© copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
  3.  
  4.  
  5. /*
  6. 11/14/93 DW: last minute pre-3.0 change -- the Run button is always
  7. enabled. we were making it conditional on whether any files were dropped
  8. onto the app. the Upgrader wants to become a droplet, and it doesn't
  9. get anything dropped on it. seems that it was hasty to assume that droplets
  10. might want to run even if nothing was dropped on it. why not use a desktop
  11. script you ask? droplets do a lot more for you!
  12.  
  13. 7/19/94 dmb: Updated for Univeral Headers
  14. */
  15.  
  16. #include <AppleEvents.h>
  17. #include <Aliases.h>
  18. #include <GestaltEqu.h>
  19. #include <Folders.h>
  20. #include <Icons.h>
  21. #include <iac.h>
  22. #include <menusharing.h>
  23.  
  24.  
  25. static DialogPtr mainwindow = nil;
  26.  
  27.  
  28. #define modelessid 130
  29. #define runitem 1
  30. #define cancelitem 2
  31. #define msgitem 3
  32. #define edititem 4
  33. #define iconitem 5
  34. #define titleitem 6
  35.     
  36. #define applemenu 128
  37. #define aboutitem 1
  38.  
  39. #define filemenu 129
  40. #define quititem 1
  41.  
  42. static MenuHandle happlemenu, hfilemenu;
  43.  
  44. Boolean flexitmainloop = false;
  45.  
  46.  
  47. typedef unsigned char byte;
  48.  
  49. static Boolean faceless = false; /*Droplet 3.0 -- 10/1/93 DW*/
  50.  
  51. static Str255 apppath; /*the full path to the droplet application*/
  52.  
  53. static Str255 appname; /*just the name of the file, no path*/
  54.  
  55. static Boolean flopenhandled = false; /*when true we fall thru the main event loop*/
  56.  
  57. static AEDesc filelist; /*holds the list of files to be processed*/
  58.  
  59. static Boolean fl2click = true; /*true if we're launched by 2clicking*/
  60.     
  61. static Str255 titlestring; /*The "XXX" Droplet*/
  62.  
  63. static Handle helptext; /*text that's displayed inside the box*/
  64.  
  65. static Boolean flcurrentlyactive = true; /*are we the active process, based on OS events*/
  66.  
  67. static Boolean flinstalled = false; /*true, once we've installed the table and menu in Frontier*/
  68.  
  69.  
  70.  
  71.  
  72. static void initmenus (void) {
  73.     
  74.     /*
  75.     set up our apple and file menus.  nothing fancy.
  76.     */
  77.     
  78.     happlemenu = GetMenu (applemenu); 
  79.     
  80.     AddResMenu (happlemenu, 'DRVR'); 
  81.     
  82.     InsertMenu (happlemenu, 0); 
  83.     
  84.     hfilemenu = GetMenu (filemenu); 
  85.     
  86.     InsertMenu (hfilemenu, 0);
  87.     
  88.     DrawMenuBar ();
  89.     } /*initmenus*/
  90.     
  91.     
  92. static void boldenbutton (DialogPtr pdialog, short itemnumber) {
  93.  
  94.     short itemtype;
  95.     Handle itemhandle;
  96.     Rect itemrect;
  97.     PenState savedpen;
  98.     
  99.     GetPenState (&savedpen); /*save the old pen state*/
  100.     
  101.     GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect); /*get the item’s rect*/
  102.     
  103.     InsetRect (&itemrect, -4, -4);
  104.     
  105.     PenSize (3, 3); /*make the pen fatter*/
  106.     
  107.     FrameRoundRect (&itemrect, 16, 16); /*draw the ring*/
  108.  
  109.     SetPenState (&savedpen); /*restore the pen state*/
  110.     } /*boldenbutton*/
  111.     
  112.     
  113. static Boolean frontmostapp () {
  114.     
  115.     ProcessSerialNumber currentprocess, frontprocess;
  116.     Boolean fl;
  117.     
  118.     GetCurrentProcess (¤tprocess);
  119.     
  120.     GetFrontProcess (&frontprocess);
  121.     
  122.     SameProcess (¤tprocess, &frontprocess, &fl);
  123.     
  124.     return (fl);
  125.     } /*frontmostapp*/
  126.     
  127.     
  128. static void cometofront (void) {
  129.  
  130.     if (!frontmostapp ()) {
  131.     
  132.         register short i;
  133.         ProcessSerialNumber psn;
  134.         EventRecord ev;
  135.         
  136.         GetCurrentProcess (&psn);
  137.         
  138.         SetFrontProcess (&psn);
  139.         
  140.         for (i = 1; i <= 3; i++)
  141.             WaitNextEvent (nullEvent, &ev, 1, nil);    
  142.         }
  143.     } /*cometofront*/
  144.     
  145.     
  146. static void notifyuser (Str255 s) {
  147.     
  148.     DialogPtr pdialog;
  149.     short itemnumber;
  150.     
  151.     cometofront ();
  152.     
  153.     ParamText (s, "\p", "\p", "\p"); 
  154.     
  155.     pdialog = GetNewDialog (129, nil, (DialogPtr) -1L);
  156.     
  157.     ShowWindow (pdialog);    
  158.     
  159.     SetPort (pdialog);
  160.     
  161.     boldenbutton (pdialog, 1);
  162.  
  163.     SysBeep (1); /*multimedia!*/
  164.     
  165.     ModalDialog (nil, &itemnumber);
  166.  
  167.     DisposDialog (pdialog);
  168.     } /*notifyuser*/
  169.  
  170.  
  171. static Boolean getFaceless (void) {
  172.     
  173.     /*
  174.     return true if this is a faceless droplet
  175.     */
  176.     
  177.     StringHandle hstring;
  178.     
  179.     hstring = GetString (129); 
  180.     
  181.     return (hstring != nil);
  182.     } /*getFaceless*/
  183.     
  184.     
  185. static Handle getResourceAndDetach (OSType restype, short resid, Boolean errorifnil) { 
  186.  
  187.     Handle h;
  188.     
  189.     h = GetResource (restype, resid);
  190.     
  191.     if (h != nil)
  192.         DetachResource (h);
  193.     else {
  194.         if (errorifnil) {
  195.         
  196.             notifyuser ("\pError loading a resource. Suggestion: increase memory for the droplet application using the Finder and try again.");
  197.         
  198.             ExitToShell ();
  199.             }
  200.         }
  201.         
  202.     return (h);
  203.     } /*getResourceAndDetach*/
  204.     
  205.     
  206. static void copystring (void *source, void *dest) {
  207.  
  208.     /*
  209.     create a copy of source in dest.  copy the length byte and
  210.     all the characters in the source string.
  211.  
  212.     assume the strings are pascal strings, with the length byte in
  213.     the first character of the string.
  214.     */
  215.     
  216.     register short n;
  217.     register byte *s = source;
  218.     register byte *d = dest;
  219.     
  220.     n = (short) s [0];
  221.     
  222.     while (n-- >= 0)
  223.         *d++ = *s++;
  224.     } /*copystring*/
  225.  
  226.  
  227. static Boolean pushstring (Str255 bssource, Str255 bsdest) {
  228.  
  229.     register short lensource = bssource [0];
  230.     register short lendest = bsdest [0];
  231.     register byte *psource, *pdest;
  232.     
  233.     if ((lensource + lendest) > 255)
  234.         return (false);
  235.     
  236.     pdest = (byte *) bsdest + (byte) lendest + 1;
  237.     
  238.     psource = (byte *) bssource + 1;
  239.     
  240.     bsdest [0] += (byte) lensource;
  241.     
  242.     while (--lensource >= 0)
  243.         *pdest++ = *psource++;
  244.     
  245.     return (true);
  246.     } /*pushstring*/
  247.     
  248.     
  249. static Boolean pushlong (long num, Str255 bsdest) {
  250.  
  251.     Str255 bsint;
  252.     
  253.     NumToString (num, bsint);
  254.     
  255.     return (pushstring (bsint, bsdest));
  256.     } /*pushlong*/
  257.     
  258.  
  259. static void clearbytes (void *pclear, long ctclear) {
  260.     
  261.     /*
  262.     do a mass memory clear
  263.     */
  264.     
  265.     register byte *p = pclear;
  266.     register long ct = ctclear;
  267.     
  268.     while (--ct >= 0)
  269.         *p++ = (byte) 0; /*tight loop*/
  270.     } /*clearbytes*/
  271.  
  272.  
  273. typedef struct versionRecord {
  274.  
  275.     unsigned short majorRev: 8; 
  276.     
  277.     unsigned short minorRev: 4;    
  278.     
  279.     unsigned short bugFixRev: 4; 
  280.     
  281.     unsigned short reserved: 15;
  282.     
  283.     unsigned short flFrontier: 1; /*true if it's Frontier, false if it's Runtime*/
  284.     } versionRecord;
  285.  
  286.  
  287. static Boolean getFrontierVersion (short *majorRev, short *minorRev, short *bugFixRev, Boolean *flRuntime) {
  288.     
  289.     /*
  290.     if Frontier isn't running, return false.
  291.     
  292.     if it is, return true with information about the version of Frontier that's running.
  293.     
  294.     About the Apple Event we send...
  295.     
  296.     We call a system event handler, so it's very fast. 
  297.     
  298.     It takes no parameters, and returns a long value. The high word of the long is the version 
  299.     number, packed the same way as the system version is packed into the SysEnvirons record. 
  300.     (8 bits major version, 4 bits minor version, 4 bits revision. The version 2.1.1 would 
  301.     be 0x0211.) The low word contains attributes of the server program. At this point only 
  302.     a single bit is defined: the low order bit is set if Frontier is the server; otherwise, 
  303.     Runtime is the server.
  304.     */
  305.     
  306.     AppleEvent event, reply;
  307.     Boolean flhavereply = false;
  308.     versionRecord x;
  309.     
  310.     if (!IAChandlerinstalled ('LAND', 'who?', true)) { /*it's Frontier 1.0, not Runtime*/
  311.         
  312.         *majorRev = 1;
  313.         
  314.         *minorRev = 0;
  315.         
  316.         *bugFixRev = 0;
  317.         
  318.         *flRuntime = false;
  319.         
  320.         return (true);
  321.         }
  322.  
  323.     if (!IACnewsystemverb ('LAND', 'who?', &event))
  324.         return (false);
  325.     
  326.     if (!IACsendverb (&event, &reply))
  327.         goto error;
  328.     
  329.     flhavereply = true;
  330.     
  331.     IACglobals.reply = &reply;
  332.     
  333.     IACglobals.event = &reply; /*get the string from the reply record*/
  334.         
  335.     if (!IACgetlongparam ('----', (long *) &x))
  336.         goto error;
  337.     
  338.     *majorRev = x.majorRev;
  339.     
  340.     *minorRev = x.minorRev;
  341.     
  342.     *bugFixRev = x.bugFixRev;
  343.     
  344.     *flRuntime = !x.flFrontier;
  345.     
  346.     AEDisposeDesc (&event);    
  347.     
  348.     AEDisposeDesc (&reply);
  349.     
  350.     return (true);
  351.     
  352.     error:
  353.     
  354.     AEDisposeDesc (&event);    
  355.     
  356.     if (flhavereply)
  357.         AEDisposeDesc (&reply);
  358.     
  359.     return (false);
  360.     } /*getFrontierVersion*/
  361.  
  362.  
  363. static Boolean IACstringtotext (Str255 s, Handle *htext) {
  364.     
  365.     register long len;
  366.     register Handle h;
  367.     
  368.     len = s [0];
  369.     
  370.     h = NewHandle (len);
  371.     
  372.     if (h == nil)
  373.         return (false);
  374.         
  375.     BlockMove (&s [1], *h, len);
  376.     
  377.     *htext = h;
  378.     
  379.     return (true);
  380.     } /*IACstringtotext*/
  381.     
  382.     
  383. static Boolean IACcheckerror (Str255 errorstring) {
  384.     
  385.     if (IACiserrorreply (errorstring)) {
  386.         
  387.         notifyuser (errorstring);
  388.         
  389.         return (true);
  390.         }
  391.         
  392.     return (false);
  393.     } /*IACcheckerror*/
  394.     
  395.     
  396. static Boolean isfolder (Str255 fullpath) {
  397.     
  398.     CInfoPBRec pb;
  399.     OSErr ec;
  400.     
  401.     clearbytes (&pb, (long) sizeof (pb)); /*init all fields to zero*/
  402.     
  403.     pb.hFileInfo.ioNamePtr = fullpath;
  404.  
  405.     pb.hFileInfo.ioVRefNum = 0;
  406.     
  407.     ec = PBGetCatInfo (&pb, false);
  408.     
  409.     if (ec != noErr)
  410.         return (false);
  411.     
  412.     return (BitTst (&pb.dirInfo.ioFlAttrib, 3));
  413.     } /*isfolder*/
  414.  
  415.  
  416. static Boolean directorytopath (long dirid, short vnum, Str255 path) {
  417.     
  418.     CInfoPBRec block;
  419.     Str255 bsdirectory;
  420.     OSErr errcode;
  421.     
  422.     path [0] = (byte) 0; /*0-length string*/
  423.     
  424.     clearbytes (&block, (long) sizeof (block));
  425.     
  426.     block.dirInfo.ioNamePtr = bsdirectory;
  427.     
  428.     block.dirInfo.ioDrParID = dirid;
  429.     
  430.     do {
  431.         block.dirInfo.ioVRefNum = vnum;
  432.         
  433.         block.dirInfo.ioFDirIndex = -1;
  434.         
  435.         block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
  436.         
  437.         errcode = PBGetCatInfo (&block,false);
  438.         
  439.         if (errcode != noErr)
  440.             return (false);
  441.             
  442.         if (!pushstring ("\p:", bsdirectory))
  443.             return (false);
  444.         
  445.         if (!pushstring (path, bsdirectory))
  446.             return (false);
  447.         
  448.         copystring (bsdirectory, path);
  449.         } while (block.dirInfo.ioDrDirID != fsRtDirID);
  450.     
  451.     return (true);
  452.     } /*directorytopath*/
  453.  
  454.  
  455. static Boolean getfullpath (FSSpec *fs, Str255 fullpath) {
  456.  
  457.     if ((*fs).parID == 1) { /*it's a volume*/
  458.         
  459.         copystring ((*fs).name, fullpath);
  460.         
  461.         pushstring ("\p:", fullpath);
  462.         }
  463.     else {
  464.         if (!directorytopath ((*fs).parID, (*fs).vRefNum, fullpath)) 
  465.             return (false);
  466.         
  467.         pushstring ((*fs).name, fullpath);
  468.         
  469.         if (isfolder (fullpath))
  470.             pushstring ("\p:", fullpath);        
  471.         }
  472.         
  473.     return (true);
  474.     } /*getfullpath*/
  475.     
  476.  
  477. static OSType GetProcessFullPath (void) {
  478.     
  479.     ProcessSerialNumber psn;
  480.     ProcessInfoRec info;
  481.     FSSpec fss;
  482.     
  483.     GetCurrentProcess (&psn);
  484.     
  485.     info.processInfoLength = (long) sizeof (info);
  486.     
  487.     info.processName = appname; /*place to store process name*/
  488.     
  489.     info.processAppSpec = &fss; /*place to store process filespec*/
  490.     
  491.     GetProcessInformation (&psn, &info);
  492.     
  493.     return (getfullpath (&fss, apppath));
  494.     } /*GetProcessFullPath*/
  495.     
  496.     
  497. static Boolean processfile (Str255 fullpath) {
  498.  
  499.     Boolean flhavereply = false;
  500.     AppleEvent event, reply;
  501.     Str255 bserror;
  502.     
  503.     if (!IACnewverb ('LAND', 'OHIO', 'proc', &event))
  504.         return (false);
  505.     
  506.     IACglobals.event = &event;
  507.     
  508.     if (!IACpushstringparam (fullpath, '----'))
  509.         return (false);
  510.         
  511.     if (!IACsendverb (&event, &reply))
  512.         goto error;
  513.     
  514.     flhavereply = true;
  515.     
  516.     IACglobals.reply = &reply;
  517.     
  518.     if (IACcheckerror (bserror)) 
  519.         goto error;
  520.         
  521.     AEDisposeDesc (&event);    
  522.     
  523.     AEDisposeDesc (&reply);
  524.     
  525.     return (true);
  526.     
  527.     error:
  528.     
  529.     AEDisposeDesc (&event);    
  530.     
  531.     if (flhavereply)
  532.         AEDisposeDesc (&reply);
  533.     
  534.     return (false);
  535.     } /*processfile*/
  536.     
  537.     
  538. static void doclosedown (void) {
  539.     
  540.     AppleEvent event, reply;
  541.     
  542.     if (IACnewverb ('LAND', 'OHIO', 'clos', &event))    
  543.         IACsendverb (&event, &reply);
  544.     } /*doclosedown*/
  545.  
  546.  
  547. static Boolean doinstall (void) {
  548.  
  549.     AEDesc desc;
  550.     OSErr ec;
  551.     Boolean flhavereply = false;
  552.     AppleEvent event, reply;
  553.     Str255 bserror;
  554.         
  555.     if (!IACnewverb ('LAND', 'OHIO', 'init', &event))
  556.         return (false);
  557.         
  558.     IACglobals.event = &event;
  559.     
  560.     desc.descriptorType = 'scpt';
  561.     
  562.     desc.dataHandle = getResourceAndDetach ('scpt', 128, true);
  563.     
  564.     ec = AEPutParamDesc (&event, (AEKeyword) 'prm1', &desc);
  565.     
  566.     AEDisposeDesc (&desc);
  567.     
  568.     if (ec != noErr)
  569.         return (false);
  570.     
  571.     desc.descriptorType = 'mbar';
  572.     
  573.     desc.dataHandle = getResourceAndDetach ('mbar', 128, false);
  574.     
  575.     ec = AEPutParamDesc (&event, (AEKeyword) 'prm2', &desc);
  576.     
  577.     AEDisposeDesc (&desc);
  578.     
  579.     if (ec != noErr)
  580.         return (false);
  581.     
  582.     if (!IACpushstringparam (apppath, 'prm3'))
  583.         return (false);
  584.         
  585.     if (!IACsendverb (&event, &reply))
  586.         goto error;
  587.     
  588.     flhavereply = true;
  589.     
  590.     IACglobals.reply = &reply;
  591.     
  592.     if (IACcheckerror (bserror)) /*syntax error or runtime error*/
  593.         goto error;
  594.         
  595.     IACglobals.event = &reply; /*get the string from the reply record*/
  596.         
  597.     AEDisposeDesc (&event);    
  598.     
  599.     AEDisposeDesc (&reply);
  600.     
  601.     return (true);
  602.     
  603.     error:
  604.     
  605.     AEDisposeDesc (&event);    
  606.     
  607.     if (flhavereply)
  608.         AEDisposeDesc (&reply);
  609.     
  610.     return (false);
  611.     } /*doinstall*/
  612.     
  613.     
  614. static Boolean dohelptext (void) {
  615.  
  616.     AEDesc desc;
  617.     OSErr ec;
  618.     Boolean flhavereply = false;
  619.     AppleEvent event, reply;
  620.     Str255 bserror;
  621.     
  622.     if (!IACnewverb ('LAND', 'OHIO', 'help', &event))
  623.         return (false);
  624.         
  625.     IACglobals.event = &event;
  626.     
  627.     desc.descriptorType = 'wptx';
  628.     
  629.     desc.dataHandle = getResourceAndDetach ('wptx', 128, false);
  630.     
  631.     ec = AEPutParamDesc (&event, (AEKeyword) '----', &desc);
  632.     
  633.     AEDisposeDesc (&desc);
  634.     
  635.     if (ec != noErr)
  636.         return (false);
  637.         
  638.     desc.descriptorType = 'TEXT'; /*DW 7/21/92 -- for Runtime, push plain text on the AE*/
  639.     
  640.     desc.dataHandle = getResourceAndDetach ('TEXT', 128, true);
  641.     
  642.     ec = AEPutParamDesc (&event, (AEKeyword) 'RUNT', &desc);
  643.     
  644.     AEDisposeDesc (&desc);
  645.     
  646.     if (ec != noErr)
  647.         return (false);
  648.         
  649.     if (!IACsendverb (&event, &reply))
  650.         goto error;
  651.     
  652.     flhavereply = true;
  653.     
  654.     IACglobals.reply = &reply;
  655.     
  656.     if (IACcheckerror (bserror)) /*syntax error or runtime error*/
  657.         goto error;
  658.         
  659.     IACglobals.event = &reply; /*get the string from the reply record*/
  660.         
  661.     AEDisposeDesc (&event);    
  662.     
  663.     AEDisposeDesc (&reply);
  664.     
  665.     return (true);
  666.     
  667.     error:
  668.     
  669.     AEDisposeDesc (&event);    
  670.     
  671.     if (flhavereply)
  672.         AEDisposeDesc (&reply);
  673.     
  674.     return (false);
  675.     } /*dohelptext*/
  676.     
  677.     
  678. static Boolean doedit (void) {
  679.  
  680.     Boolean flhavereply = false;
  681.     AppleEvent event, reply;
  682.     Str255 bserror;
  683.     
  684.     if (!IACnewverb ('LAND', 'OHIO', 'edit', &event))
  685.         return (false);
  686.         
  687.     if (!IACsendverb (&event, &reply))
  688.         goto error;
  689.     
  690.     flhavereply = true;
  691.     
  692.     IACglobals.reply = &reply;
  693.     
  694.     if (IACcheckerror (bserror)) /*syntax error or runtime error*/
  695.         goto error;
  696.         
  697.     IACglobals.event = &reply; /*get the string from the reply record*/
  698.         
  699.     AEDisposeDesc (&event);    
  700.     
  701.     AEDisposeDesc (&reply);
  702.     
  703.     flexitmainloop = true;
  704.     
  705.     return (true);
  706.     
  707.     error:
  708.     
  709.     AEDisposeDesc (&event);    
  710.     
  711.     if (flhavereply)
  712.         AEDisposeDesc (&reply);
  713.     
  714.     return (false);
  715.     } /*doedit*/
  716.     
  717.     
  718. static Boolean dofilelist (void) {
  719.     
  720.     long ctfiles;
  721.     DescType actualtype;
  722.     long actualsize;
  723.     AEKeyword actualkeyword;
  724.     FSSpec fs;
  725.     long i;
  726.     OSErr ec;
  727.     Str255 fullpath;
  728.     
  729.     flopenhandled = true;
  730.     
  731.     fl2click = false; /*it was a drag-and-drop*/
  732.     
  733.     ec = AECountItems (&filelist, &ctfiles);
  734.     
  735.     if (ec != noErr) 
  736.         return (ec);
  737.     
  738.     if (ctfiles <= 0)
  739.         return (noErr);
  740.     
  741.     for (i = 1; i <= ctfiles; i ++) {
  742.         
  743.         ec = AEGetNthPtr (
  744.             &filelist, i, typeFSS, &actualkeyword, &actualtype, 
  745.             
  746.             (Ptr) &fs, sizeof (fs), &actualsize);
  747.         
  748.         getfullpath (&fs, fullpath);
  749.     
  750.         if (!processfile (fullpath)) {
  751.             
  752.             doclosedown ();
  753.             
  754.             return (-1);
  755.             }
  756.         } /*for*/
  757.     
  758.     doclosedown ();
  759.     
  760.     return (noErr);
  761.     } /*dofilelist*/
  762.  
  763.  
  764. static Boolean appRunning (OSType appid) {
  765.     
  766.     /*
  767.     return true if the server application is running. 
  768.     */
  769.     
  770.     ProcessInfoRec info;
  771.     ProcessSerialNumber psn;
  772.     Str255 bsname;
  773.     FSSpec fss;
  774.     
  775.     info.processInfoLength = sizeof (info);
  776.     
  777.     info.processName = bsname; /*place to store process name*/
  778.     
  779.     info.processAppSpec = &fss; /*place to store process filespec*/
  780.     
  781.     psn.highLongOfPSN = kNoProcess;
  782.     
  783.     psn.lowLongOfPSN = kNoProcess;
  784.     
  785.     while (GetNextProcess (&psn) == noErr) {
  786.         
  787.          info.processInfoLength = sizeof (ProcessInfoRec);
  788.          
  789.         if (GetProcessInformation (&psn, &info) != noErr)
  790.             continue; /*keep going -- ignore error*/
  791.         
  792.         if (info.processSignature == appid)
  793.             return (true);
  794.         } /*while*/
  795.     
  796.     return (false); /*loop completed, no server*/
  797.     } /*appRunning*/
  798.  
  799.  
  800. static Boolean hasdesktopmanager (short vnum) {
  801.     
  802.     GetVolParmsInfoBuffer volparms;
  803.     HParamBlockRec pb;
  804.     
  805.     pb.volumeParam.ioVRefNum = vnum;
  806.     
  807.     pb.volumeParam.ioNamePtr = nil;
  808.     
  809.     pb.ioParam.ioBuffer = (Ptr) &volparms;
  810.     
  811.     pb.ioParam.ioReqCount = sizeof (volparms);
  812.     
  813.     if (PBHGetVolParmsSync (&pb) != noErr)
  814.         return (false);
  815.     
  816.     return ((volparms.vMAttrib & (1 << bHasDesktopMgr)) != 0);
  817.     } /*hasdesktopmanager*/
  818.  
  819.  
  820. static Boolean findApp (OSType appid, FSSpec *fs) {
  821.     
  822.     /*
  823.     find the app whose creator id is appid. return true if we found it, 
  824.     false otherwise.
  825.     */
  826.     
  827.     DTPBRec dt;
  828.     ParamBlockRec pb;
  829.     Str255 appfname;
  830.     
  831.     clearbytes (&pb, (long) sizeof (pb));
  832.     
  833.     while (true) {
  834.         
  835.         ++pb.volumeParam.ioVolIndex;
  836.         
  837.         if (PBGetVInfoSync (&pb) != noErr)
  838.             return (false);
  839.         
  840.         if (!hasdesktopmanager (pb.volumeParam.ioVRefNum))
  841.             continue;
  842.         
  843.         dt.ioNamePtr = NULL;
  844.         
  845.         dt.ioVRefNum = pb.volumeParam.ioVRefNum;
  846.         
  847.         if (PBDTGetPath (&dt) != noErr)
  848.             return (false);
  849.         
  850.         dt.ioNamePtr = (StringPtr) &appfname;
  851.         
  852.         dt.ioIndex = 0;
  853.         
  854.         dt.ioFileCreator = appid;
  855.         
  856.         if (PBDTGetAPPLSync (&dt) == noErr) {
  857.         
  858.             if (FSMakeFSSpec (pb.volumeParam.ioVRefNum, dt.ioAPPLParID, appfname, fs) == noErr)
  859.                 return (true);
  860.             }
  861.         } /*while*/
  862.         
  863.     return (false);
  864.     } /*findApp*/
  865.  
  866.  
  867. static Boolean launchApp (FSSpec *fs) {
  868.     
  869.     LaunchParamBlockRec pb;
  870.     
  871.     clearbytes (&pb, (long) sizeof (pb));
  872.     
  873.     pb.launchAppSpec = fs;
  874.     
  875.     pb.launchBlockID = extendedBlock;
  876.     
  877.     pb.launchEPBLength = extendedBlockLen;
  878.     
  879.     pb.launchControlFlags = launchContinue | launchNoFileFlags/* | launchDontSwitch*/;
  880.     
  881.     return (LaunchApplication (&pb) == noErr);
  882.     } /*launchApp*/
  883.  
  884.  
  885. static Boolean launchServer (OSType serverid) {
  886.     
  887.     /*
  888.     if the application whose creator id is serverid is running, return true.
  889.     
  890.     if not, we look for the application and try to launch it. we wait until it's 
  891.     actually running and ready to receive Apple Events.
  892.     */
  893.     
  894.     FSSpec fs;
  895.     
  896.     if (appRunning (serverid))
  897.         return (true);
  898.         
  899.     if (!findApp (serverid, &fs))
  900.         return (false);
  901.         
  902.     if (!launchApp (&fs))
  903.         return (false);
  904.         
  905.     while (!appRunning (serverid)) {
  906.         
  907.         EventRecord ev;
  908.         
  909.         EventAvail (everyEvent, &ev);
  910.         } /*while*/
  911.         
  912.     return (true);
  913.     } /*launchServer*/
  914.     
  915.     
  916. static pascal OSErr handleopen (AppleEvent *event, AppleEvent *reply, long refcon) {
  917.     
  918.     OSErr ec;
  919.     
  920.     flopenhandled = true;
  921.     
  922.     fl2click = false; /*it was a drag-and-drop*/
  923.     
  924.     ec = AEGetKeyDesc (event, keyDirectObject, typeAEList, &filelist);
  925.     
  926.     return (ec);
  927.     } /*handleopen*/
  928.  
  929.  
  930. static pascal OSErr handleopenapp (AppleEvent *event, AppleEvent *reply, long refcon) {
  931.     
  932.     flopenhandled = true;
  933.     
  934.     fl2click = true; /*launched by 2clicking*/
  935.     
  936.     return (noErr);
  937.     } /*handleopenapp*/
  938.     
  939.  
  940. static void drawitemtext (DialogPtr pdialog, short itemnumber, Handle htext) {
  941.  
  942.     short itemtype;
  943.     Handle itemhandle;
  944.     Rect itemrect;
  945.     short savedfont, savedsize;
  946.     Rect r;
  947.     PenState savedpen;
  948.     short fnum;
  949.     
  950.     savedfont = (*qd.thePort).txFont;
  951.     
  952.     savedsize = (*qd.thePort).txSize;
  953.     
  954.     GetFNum ("\pPalatino", &fnum);
  955.     
  956.     TextFont (fnum);
  957.     
  958.     TextSize (12);
  959.     
  960.     GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect); /*get the item’s rect*/
  961.     
  962.     r = itemrect;
  963.     
  964.     InsetRect (&r, 3, 3);
  965.     
  966.     TextBox (*htext, GetHandleSize (htext), &r, 0);
  967.     
  968.     TextFont (savedfont);
  969.     
  970.     TextSize (savedsize);
  971.     
  972.     GetPenState (&savedpen); /*save the old pen state*/
  973.     
  974.     PenPat (&qd.gray);
  975.     
  976.     FrameRect (&itemrect);
  977.     
  978.     SetPenState (&savedpen);
  979.     } /*drawitemtext*/
  980.     
  981.  
  982. static void drawitemstring (DialogPtr pdialog, short itemnumber, Str255 s) {
  983.  
  984.     short itemtype;
  985.     Handle itemhandle;
  986.     Rect itemrect;
  987.     short savedfont, savedsize, savedstyle;
  988.     Rect r;
  989.     PenState savedpen;
  990.     FontInfo info;
  991.     RgnHandle rgn;
  992.         
  993.     savedfont = (*qd.thePort).txFont;
  994.     
  995.     savedsize = (*qd.thePort).txSize;
  996.     
  997.     savedstyle = (*qd.thePort).txFace;
  998.     
  999.     TextFont (helvetica);
  1000.     
  1001.     TextSize (18);
  1002.     
  1003.     TextFace (bold);
  1004.     
  1005.     GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect); /*get the item’s rect*/
  1006.     
  1007.     r = itemrect;
  1008.     
  1009.     GetFontInfo (&info);
  1010.     
  1011.     MoveTo (r.left, r.bottom - info.descent);
  1012.     
  1013.     GetClip (rgn = NewRgn ());
  1014.     
  1015.     ClipRect (&r);
  1016.     
  1017.     DrawString (s);
  1018.     
  1019.     SetClip (rgn);
  1020.     
  1021.     DisposeRgn (rgn);
  1022.     
  1023.     TextFont (savedfont);
  1024.     
  1025.     TextSize (savedsize);
  1026.     
  1027.     TextFace (savedstyle);
  1028.     
  1029.     GetPenState (&savedpen); /*save the old pen state*/
  1030.     
  1031.     PenPat (&qd.gray);
  1032.     
  1033.     SetPenState (&savedpen);
  1034.     } /*drawitemstring*/
  1035.     
  1036.  
  1037. static void drawitemicon (DialogPtr pdialog, short itemnumber, short resid) {
  1038.  
  1039.     short itemtype;
  1040.     Handle itemhandle;
  1041.     Rect itemrect;
  1042.         
  1043.     GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect); /*get the item’s rect*/
  1044.     
  1045.     PlotIconID (&itemrect, 0, 0, resid);
  1046.     } /*drawitemicon*/
  1047.     
  1048.     
  1049. static void grayrect (Rect r) {
  1050.  
  1051.     PenState savedpen;
  1052.     
  1053.     GetPenState (&savedpen); /*save the old pen state*/
  1054.     
  1055.     PenMode (patBic);
  1056.     
  1057.     PenPat (&qd.gray);
  1058.     
  1059.     PaintRect (&r);
  1060.     
  1061.     SetPenState (&savedpen);
  1062.     } /*grayrect*/
  1063.     
  1064.     
  1065. static void disabledialogitem (DialogPtr pdialog, short itemnumber) {
  1066.     
  1067.     /*
  1068.     new version of this routine, thanks to Mike Alexander of Ann Arbor, MI.
  1069.     
  1070.     see notes for Droplet Developer version 2.1b3.
  1071.     */
  1072.     
  1073.     short itemtype;
  1074.     ControlHandle itemhandle;
  1075.     Rect itemrect;
  1076.  
  1077.     GetDItem (pdialog, itemnumber, &itemtype, (Handle *) &itemhandle, &itemrect);
  1078.     
  1079.     if ((**itemhandle).contrlHilite == 0) /*it is enabled, disable it*/
  1080.         HiliteControl (itemhandle, 0xFF);
  1081.     } /*disabledialogitem*/
  1082.  
  1083.  
  1084. static void highlightbutton (DialogPtr w, short itemnumber, Boolean flon) {
  1085.     
  1086.     short x;
  1087.     short itemtype;
  1088.     Handle itemhandle;
  1089.     Rect itembox;
  1090.     
  1091.     GetDItem (w, itemnumber, &itemtype, &itemhandle, &itembox);
  1092.     
  1093.     if (flon)
  1094.         x = inButton;
  1095.     else
  1096.         x = 0;
  1097.     
  1098.     HiliteControl ((ControlHandle) itemhandle, x); 
  1099.     } /*highlightbutton*/
  1100.  
  1101.  
  1102. static void delayticks (short ct) {
  1103.     
  1104.     long tc;
  1105.     
  1106.     tc = TickCount () + ct;
  1107.     
  1108.     while (TickCount () < tc) {}
  1109.     } /*delayticks*/
  1110.     
  1111.     
  1112. static void simulatehit (DialogPtr w, short item) {
  1113.     
  1114.     highlightbutton (w, item, true);
  1115.     
  1116.     delayticks (8);
  1117.     
  1118.     highlightbutton (w, item, false);
  1119.     } /*simulatehit*/
  1120.  
  1121.  
  1122. static void handledrag (EventRecord *ev, WindowPtr w) {
  1123.     
  1124.     Rect r;
  1125.  
  1126.     r = qd.screenBits.bounds; 
  1127.     
  1128.     r.top = r.top + GetMBarHeight (); 
  1129.     
  1130.     InsetRect (&r, 4, 4);
  1131.     
  1132.     DragWindow (w, (*ev).where, &r);
  1133.     } /*handledrag*/
  1134.  
  1135.  
  1136. static void handlemenu (long codeword) {
  1137.     
  1138.     register short idmenu, iditem;
  1139.     
  1140.     iditem = LoWord (codeword);
  1141.     
  1142.     idmenu = HiWord (codeword);
  1143.     
  1144.     if (SharedMenuHit (idmenu, iditem)) 
  1145.         goto exit;
  1146.     
  1147.     switch (idmenu) {
  1148.     
  1149.         case applemenu: 
  1150.             switch (iditem) {
  1151.                 
  1152.                 case aboutitem:
  1153.                     notifyuser (*GetString (130));
  1154.                     
  1155.                     break;
  1156.             
  1157.                 default: {
  1158.                     Str255 s;
  1159.                     
  1160.                     GetItem (happlemenu, iditem, s);
  1161.                     
  1162.                     OpenDeskAcc (s);
  1163.                     
  1164.                     break;
  1165.                     }
  1166.                 } /*switch*/
  1167.             
  1168.             break; /*apple menu*/
  1169.  
  1170.         case filemenu: 
  1171.             switch (iditem) {
  1172.                 
  1173.                 case quititem:
  1174.                 
  1175.                     flexitmainloop = true;
  1176.                     
  1177.                     break;
  1178.                 } /*switch*/
  1179.             
  1180.             break; /*file menu*/
  1181.             
  1182.         } /*switching on which menu was invoked*/
  1183.         
  1184.     exit:
  1185.     
  1186.     HiliteMenu (0);
  1187.     } /*handlemenu*/
  1188.  
  1189.  
  1190. static void handlemouse (EventRecord *ev) {
  1191.  
  1192.     register short part;
  1193.     WindowPtr w;
  1194.     
  1195.     if (IsDialogEvent (ev)) {
  1196.         
  1197.         DialogPtr pdialog;
  1198.         short itemhit;
  1199.  
  1200.         if (DialogSelect (ev, &pdialog, &itemhit)) {
  1201.             
  1202.             switch (itemhit) {
  1203.                 
  1204.                 case runitem:
  1205.                     DisposDialog (mainwindow);
  1206.                     
  1207.                     mainwindow = nil;
  1208.                     
  1209.                     dofilelist ();
  1210.                     
  1211.                     flexitmainloop = true;
  1212.                     
  1213.                     break;
  1214.                 
  1215.                 case cancelitem:
  1216.                     flexitmainloop = true;
  1217.                     
  1218.                     break;
  1219.                     
  1220.                 case edititem:
  1221.                     doedit ();
  1222.                     
  1223.                     break;
  1224.                 } /*switch*/
  1225.             }
  1226.         
  1227.         return;
  1228.         }
  1229.     
  1230.     part = FindWindow ((*ev).where, &w);
  1231.     
  1232.     if (w != nil) 
  1233.     
  1234.         if (w != FrontWindow ()) { /*just like all other Mac programs*/
  1235.             
  1236.             SelectWindow (w);
  1237.                             
  1238.             return; /*the mouse click is consumed by the bringtofront operation*/
  1239.             }
  1240.     
  1241.     switch (part) {
  1242.     
  1243.         case inMenuBar: 
  1244.             handlemenu (MenuSelect ((*ev).where)); 
  1245.             
  1246.             break;
  1247.         
  1248.         case inSysWindow:
  1249.             SystemClick (ev, w); 
  1250.             
  1251.             break;
  1252.         
  1253.         case inDrag:
  1254.             handledrag (ev, w);
  1255.             
  1256.             break;
  1257.         } /*switch*/
  1258.     } /*handlemouse*/
  1259.     
  1260.     
  1261. static Boolean handlekeystroke (EventRecord *ev) { 
  1262.  
  1263.     char ch = (*ev).message & charCodeMask;
  1264.     Boolean flcmdperiod = ((*ev).modifiers & cmdKey) && (ch == '.');
  1265.     
  1266.     if (SharedScriptRunning ()) { /*cmd-period terminates the script*/
  1267.     
  1268.         if (flcmdperiod) { 
  1269.             
  1270.             CancelSharedScript (); /*cancel the shared menu script, if one is running*/
  1271.         
  1272.             return (true);
  1273.             }
  1274.         }
  1275.     
  1276.     if ((ch == (char) 13) || (ch == (char) 3) || (ch == 'r') || (ch == 'R')) { 
  1277.         
  1278.         /*user hit Return, Enter, r or R*/
  1279.         
  1280.         if (fl2click) /*Run button not enabled*/
  1281.             return (true);
  1282.         
  1283.         simulatehit (mainwindow, runitem);
  1284.     
  1285.         DisposDialog (mainwindow);
  1286.         
  1287.         mainwindow = nil;
  1288.     
  1289.         dofilelist ();
  1290.     
  1291.         return (false);
  1292.         }
  1293.         
  1294.     if ((flcmdperiod) || ((ch == 'q') || (ch == 'Q'))) { /*close the window and exit*/
  1295.     
  1296.         simulatehit (mainwindow, cancelitem);
  1297.         
  1298.         return (false);
  1299.         }
  1300.         
  1301.     if ((ch == 'e') || (ch == 'E')) {
  1302.         
  1303.         simulatehit (mainwindow, edititem);
  1304.         
  1305.         doedit ();
  1306.         
  1307.         return (true);
  1308.         }
  1309.         
  1310.     handlemenu (MenuKey (ch)); 
  1311.     
  1312.     return (true);
  1313.     } /*handlekeystroke*/
  1314.     
  1315.     
  1316. static void handleupdate (EventRecord *ev) {
  1317.     
  1318.     WindowPtr w = (WindowPtr) (*ev).message;
  1319.     
  1320.     SetPort (w);
  1321.     
  1322.     BeginUpdate (w);
  1323.     
  1324.     DrawDialog (w);
  1325.     
  1326.     if (false) { /*11/14/93 DW -- always enable the Run button*/
  1327.     
  1328.         if (fl2click)                     
  1329.             disabledialogitem (w, runitem);
  1330.         else
  1331.             boldenbutton (w, runitem);
  1332.         }
  1333.     else
  1334.         boldenbutton (w, runitem);
  1335.     
  1336.     /*enable Edit button if it's not Runtime running*/ {
  1337.     
  1338.         short majorRev, minorRev, bugFixRev;
  1339.         Boolean flRuntime;
  1340.  
  1341.         getFrontierVersion (&majorRev, &minorRev, &bugFixRev, &flRuntime);
  1342.         
  1343.         if (flRuntime)
  1344.             disabledialogitem (w, edititem);
  1345.         }
  1346.     
  1347.     drawitemicon (w, iconitem, 128);
  1348.     
  1349.     drawitemstring (w, titleitem, titlestring);
  1350.  
  1351.     drawitemtext (w, msgitem, helptext);
  1352.     
  1353.     EndUpdate (w);
  1354.     } /*handleupdate*/
  1355.  
  1356.  
  1357. static Boolean handleevent (EventRecord *ev) {
  1358.  
  1359.     switch ((*ev).what) { 
  1360.     
  1361.         case kHighLevelEvent:
  1362.             AEProcessAppleEvent (ev);
  1363.             
  1364.             break;
  1365.             
  1366.         case osEvt:
  1367.             flcurrentlyactive = ((*ev).message & resumeFlag) != 0;
  1368.             
  1369.             break;
  1370.     
  1371.         case nullEvent: 
  1372.             if (flcurrentlyactive && flinstalled)
  1373.                 CheckSharedMenus (140);
  1374.             
  1375.             break;
  1376.  
  1377.         case updateEvt:
  1378.             handleupdate (ev);
  1379.             
  1380.             break;
  1381.             
  1382.         case keyDown: case autoKey: 
  1383.             if (!handlekeystroke (ev))
  1384.                 return (false);
  1385.             
  1386.             break;
  1387.                 
  1388.         case mouseDown:
  1389.             if (!faceless)
  1390.                 handlemouse (ev);
  1391.             
  1392.             break;            
  1393.         } /*switch*/
  1394.             
  1395.     return (true);
  1396.     } /*handleevent*/
  1397.  
  1398.  
  1399. static pascal void pascalalert (Str255 s) { /*DW 11/5/93*/
  1400.     
  1401.     notifyuser (s);
  1402.     } /*pascalalert*/
  1403.     
  1404.     
  1405. static pascal void pascaleventfilter (EventRecord *ev) { /*DW 11/5/93*/
  1406.     
  1407.     handleevent (ev);
  1408.     } /*pascaleventfilter*/
  1409.     
  1410.  
  1411. static void maineventloop (void) {
  1412.     
  1413.     EventRecord ev;
  1414.     
  1415.     if (!faceless) {
  1416.     
  1417.         InitSharedMenus (&pascalalert, &pascaleventfilter);
  1418.         
  1419.         initmenus ();
  1420.         
  1421.         helptext = getResourceAndDetach ('TEXT', 128, true);
  1422.         
  1423.         mainwindow = GetNewDialog (modelessid, nil, (WindowPtr) -1);
  1424.         
  1425.         SetPort (mainwindow);
  1426.         
  1427.         SetWTitle (mainwindow, appname);
  1428.         
  1429.         copystring ("\pThe “", titlestring);
  1430.         
  1431.         pushstring (appname, titlestring);
  1432.         
  1433.         pushstring ("\p” Droplet", titlestring);
  1434.         }
  1435.     
  1436.     while (!flexitmainloop) {
  1437.         
  1438.         if (!faceless)
  1439.             SetCursor (&qd.arrow);
  1440.         
  1441.         WaitNextEvent (everyEvent, &ev, 30, nil);
  1442.         
  1443.         if (!handleevent (&ev))
  1444.             goto endloop;
  1445.         
  1446.         if (!flinstalled) {
  1447.             
  1448.             flinstalled = true;
  1449.             
  1450.             doinstall ();
  1451.             }
  1452.         
  1453.         if (faceless) {
  1454.             
  1455.             if (flopenhandled) {
  1456.                 
  1457.                 dofilelist ();
  1458.                 
  1459.                 goto endloop;
  1460.                 }
  1461.             }
  1462.         } /*while*/
  1463.  
  1464.     endloop:
  1465.     
  1466.     if (mainwindow != nil)
  1467.         DisposDialog (mainwindow);
  1468.     } /*maineventloop*/
  1469.     
  1470.     
  1471. void main (void) {
  1472.     
  1473.     long expiresat;
  1474.     
  1475.     InitGraf (&qd.thePort);
  1476.     
  1477.     InitFonts ();
  1478.     
  1479.     FlushEvents (everyEvent, 0);
  1480.     
  1481.     InitWindows ();
  1482.     
  1483.     InitMenus ();
  1484.     
  1485.     TEInit ();
  1486.     
  1487.     InitDialogs (0L);
  1488.     
  1489.     InitCursor ();
  1490.     
  1491.     if (!IAChaveappleevents ()) 
  1492.         return;
  1493.         
  1494.     if (!IACinstallhandler (kCoreEventClass, kAEOpenApplication, (ProcPtr) &handleopenapp))
  1495.         return;
  1496.         
  1497.     if (!IACinstallhandler (kCoreEventClass, kAEOpenDocuments, (ProcPtr) &handleopen))
  1498.         return;
  1499.         
  1500.     GetProcessFullPath ();
  1501.     
  1502.     if (!launchServer ('LAND')) { /*Frontier/Runtime isn't running and couldn't be launched*/
  1503.     
  1504.         Alert (128, nil);
  1505.         
  1506.         return;
  1507.         }
  1508.     
  1509.     faceless = getFaceless (); /*DW 10/1/93*/
  1510.     
  1511.     if (faceless) {
  1512.     
  1513.         expiresat = TickCount () + (1 * 60);
  1514.         
  1515.         while (!flopenhandled) { /*wait for the opendoc Apple Event to come in*/
  1516.             
  1517.             EventRecord ev;
  1518.     
  1519.             if (WaitNextEvent (everyEvent, &ev, 30, nil)) {
  1520.                 
  1521.                 if (ev.what == kHighLevelEvent) 
  1522.                     AEProcessAppleEvent (&ev);
  1523.                 }
  1524.             
  1525.             if (TickCount () > expiresat)
  1526.                 break;
  1527.             } /*while*/
  1528.         }
  1529.     
  1530.     maineventloop ();
  1531.     } /*main*/
  1532.  
  1533.  
  1534.